package com.zby.zbyliving.commodity.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.common.utils.PageUtils;
import com.zby.common.utils.Query;

import com.zby.zbyliving.commodity.dao.CategoryDao;
import com.zby.zbyliving.commodity.entity.CategoryEntity;
import com.zby.zbyliving.commodity.service.CategoryService;

@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
//        log.info(entities.toString());
        List<CategoryEntity> categoryTree = entities.stream().
                filter(categoryEntity -> categoryEntity.getParentId() == 0
        ).map((category) -> {category.setChildrenCategories(getChildrenCategories(category, entities));
            return category;
        }).sorted((category1, category2) -> {
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
//        log.info(categoryTree.toString());
            return categoryTree;
    }
    private List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals(root.getId());
        }).map(categoryEntity -> {categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity,all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {
        List<Long> cascadedCategoryId = new ArrayList<>();
        //2. 调用方法进行处理-递归方法
        //   cascadedCategoryId是引用传递, 所以直接就影响到 本方法的 cascadedCategoryId
        //   返回的数据 如 [301,21,1]
        getParentCategoryId(categoryId, cascadedCategoryId);
        Collections.reverse(cascadedCategoryId);
        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }
    private List<Long> getParentCategoryId(Long categoryId, List<Long> cascadedCategoryId) {

        //1. 先把categoryId放入到
        cascadedCategoryId.add(categoryId);
        //2. 根据categoryId得到他的CategoryEntity
        CategoryEntity categoryEntity = this.getById(categoryId);
        //3. 判断categoryEntity的parentId是否为0, 如果不为0，说明他有上级分类
        //   就递归处理
        if (!categoryEntity.getParentId().equals(0L)) {
            //21,cascadedCategoryId=> cascadedCategoryId[301,21,1]
            getParentCategoryId(categoryEntity.getParentId(), cascadedCategoryId);
        }

        return cascadedCategoryId;
    }


}
